/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.azoft.carousellayoutmanager;

import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.Locale;

/**
 * 展示选中item的下标
 *
 * @since 2021-03-24
 */
public class ShowItemClickPositionDialog extends ToastDialog {
    private Context context;
    private Text text;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public ShowItemClickPositionDialog(Context context) {
        super(context);
        this.context = context;
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        Component component = LayoutScatter.getInstance(context).parse(
                ResourceTable.Layout_show_click_position_layout, null, false);
        text = (Text) component.findComponentById(ResourceTable.Id_txt_position);
        setComponent(component);
        setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        setAlignment(LayoutAlignment.BOTTOM);
    }

    /**
     * 设置显示的文字
     *
     * @param position 下标
     * @return dialog对象
     */
    public ShowItemClickPositionDialog setText(int position) {
        String msg = String.format(Locale.US, "Item %1$d was clicked", position);
        text.setText(msg);
        return this;
    }
}
